//
//  PPMAppleScript.m
//  livedoor Blog Helper
//
//  Created by ppm on 07/01/16.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "PPMAppleScript.h"

#define kASAppleScriptSuite	'ascr'
#define kASSubroutineEvent	'psbr'
#define keyASSubroutineName 'snam'

@implementation PPMAppleScript

+ (id)doAppleScript:(NSString *)scriptName handler:(NSString *)handlerName withParameters:(NSArray *)parameterArray
{
	
	id theResult;
	
    // load the script from a resource by fetching its URL from within our bundle
    NSString* path = [[NSBundle mainBundle] pathForResource:scriptName ofType:@"scpt" inDirectory:@"Scripts"];
	
    if (path != nil)
    {
        NSURL* url = [NSURL fileURLWithPath:path];
        if (url != nil)
        {
            NSDictionary* errors = [NSDictionary dictionary];
            NSAppleScript* appleScript =
                    [[NSAppleScript alloc] initWithContentsOfURL:url error:&errors];
            if (appleScript != nil)
            {
			
				// create and populate the list of parameters (in our case just one)
                NSAppleEventDescriptor* parameters = [NSAppleEventDescriptor listDescriptor];
				
				int i;
				
				for (i = 0 ; i < [parameterArray count] ; i++)
				{
					[parameters insertDescriptor:[NSAppleEventDescriptor descriptorWithString:[parameterArray objectAtIndex:i]] atIndex:i + 1];
				}

                // create the AppleEvent target
                ProcessSerialNumber psn = {0, kCurrentProcess};
                NSAppleEventDescriptor* target =
                [NSAppleEventDescriptor
                        descriptorWithDescriptorType:typeProcessSerialNumber
                        bytes:&psn
                        length:sizeof(ProcessSerialNumber)];

                // create an NSAppleEventDescriptor with the script's method name to call,
                // this is used for the script statement: "on show_message(user_message)"
                // Note that the routine name must be in lower case.
                NSAppleEventDescriptor* handler =
                        [NSAppleEventDescriptor descriptorWithString:
                        [handlerName lowercaseString]];

                // create the event for an AppleScript subroutine,
                // set the method name and the list of parameters
                NSAppleEventDescriptor* event =
                        [NSAppleEventDescriptor appleEventWithEventClass:kASAppleScriptSuite
                                eventID:kASSubroutineEvent
                                targetDescriptor:target
                                returnID:kAutoGenerateReturnID
                transactionID:kAnyTransactionID];
                [event setParamDescriptor:handler forKeyword:keyASSubroutineName];
                [event setParamDescriptor:parameters forKeyword:keyDirectObject];
				
				theResult = [appleScript executeAppleEvent:event error:&errors];

                [appleScript release];
            }
            else
            {
                // report any errors from 'errors'
            }
        }
    }
	
	return theResult;
	
}

@end
